package com.tianda.his.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianda.his.modules.system.dto.DictDataDto;
import com.tianda.his.modules.system.entity.SysDictData;
import com.tianda.his.modules.system.entity.SysDictType;
import com.tianda.his.modules.system.mapper.SysDictDataMapper;
import com.tianda.his.modules.system.service.DictTypeService;
import com.tianda.his.modules.system.mapper.DictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author jinken
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2025-09-15 09:29:34
*/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, SysDictType>
    implements DictTypeService{


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public Page<SysDictType> listForPage(DictDataDto dictDataDto) {
        Page<SysDictType> pageObject = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());


        //创建条件对象
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();

        //参数字典类型
        String dictType = dictDataDto.getDictType();
        //查询字典类型
        queryWrapper.like(StrUtil.isNotBlank(dictType), SysDictType::getDictType, dictType);

        //状态条件
        String status = dictDataDto.getStatus();
        queryWrapper.eq(StrUtil.isNotBlank(status), SysDictType::getStatus, status);

        //获取开始结束时间
        Date beginTime = dictDataDto.getBeginTime();
        Date endTime = dictDataDto.getEndTime();

        queryWrapper.gt(Objects.nonNull(beginTime),SysDictType::getCreateTime, beginTime);
        queryWrapper.lt(Objects.nonNull(endTime),SysDictType::getCreateTime, endTime);

        //分页查询
        this.page(pageObject,queryWrapper);

        return pageObject;
    }

    @Override
    public void addDictType(DictDataDto dictDataDto) {
        //创建字典类型对象
        SysDictType sysDictType = new SysDictType();
        sysDictType.setDictName(dictDataDto.getDictLabel());
        sysDictType.setDictType(dictDataDto.getDictType());
        sysDictType.setStatus(dictDataDto.getStatus());
        sysDictType.setRemark(dictDataDto.getRemark());
        sysDictType.setCreateTime(new Date());

        //保存数据字典类型
        this.save(sysDictType);
    }

    /**
     * 同步缓存的做法
     * 1，查询出所有可用的字典类型数据
     * 2，再根据字典的类型查询字典数据
     * 3，把字典数据生成json存到redis
     * 设计key
     * dict:dictType
     * 如dict:sys_user_sex --->[{},{},{}]
     *
     */

    @Override
    public void dictCacheAsync() {
        //查询所有的数据字典类型
        List<SysDictType> dictTypes = this.list();


        for (SysDictType sysDictType : dictTypes) {
            String dictType = sysDictType.getDictType();
            //根据数据字典类型查询对应的数据字典值
            LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, dictType);
            queryWrapper.eq(SysDictData::getStatus, 0);

            List<SysDictData> sysDictDatas = sysDictDataMapper.selectList(queryWrapper);

            //同步redis
            redisTemplate.boundHashOps("dictCache").put(dictType,sysDictDatas);
        }

    }

    @Override
    public List<SysDictType> selectAllDictType() {
//        //从redis查询
//        List<List<SysDictType>> datas = (List<List<SysDictType>>) redisTemplate.boundHashOps("dictCache").entries().values();
//        if(CollUtil.isNotEmpty(datas)){
//            List<SysDictType> dictTypes = new ArrayList<>();
//            for (List<SysDictType> data : datas) {
//                dictTypes.addAll(data);
//            }
//            return dictTypes;
//        }


        //从数据库查询
        return this.list();
    }
}




